package me.numark.fragments;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.astuetz.PagerSlidingTabStrip;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import me.numark.R;
import me.numark.net.BaseResponse;
import me.numark.net.GsonRequest;
import me.numark.net.NetworkConstants;
import me.numark.net.NetworkHelper;


/**
 * Created by tian on 14-9-18:上午7:44.
 */
public class HotFragment extends Fragment implements View.OnClickListener {

    private ViewPager mPager;

    private PagerSlidingTabStrip mTabs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_hot, container, false);
        mPager = (ViewPager) v.findViewById(R.id.pager);
        mChannelAdapter = new NewsChannelAdapter();
        mPager.setAdapter(mChannelAdapter);
        mTabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
        v.findViewById(R.id.left).setOnClickListener(this);
        v.findViewById(R.id.right).setOnClickListener(this);
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        loadChannels();
    }

    @Override
    public void onClick(View v) {
//        int pos = mPager.getCurrentItem();
//        NewsListFragment fragment = (NewsListFragment) mChannelAdapter.getItem(pos);
//        Bundle args = new Bundle();
//        switch (v.getId()) {
//            case R.id.left:
//                args.putInt(NewsListFragment.ORDER_BY, NewsListFragment.LATEST);
//                if (fragment.isAdded()) {
//                    fragment.loadCursor(args);
//                }
//                break;
//            case R.id.right:
//                args.putInt(NewsListFragment.ORDER_BY, NewsListFragment.HOTTEST);
//                if (fragment.isAdded()) {
//                    fragment.loadCursor(args);
//                }
//                break;
//        }
    }

    private Map<Integer, NewsListFragment> mFragmentMap = new HashMap<Integer, NewsListFragment>();

    private class NewsChannelAdapter extends FragmentPagerAdapter {

        public NewsChannelAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int i) {
//            int channelId = mNewsTypesResponse.data[i].id;
//            if (mFragmentMap.containsKey(channelId)) {
//                return mFragmentMap.get(channelId);
//            }
//            Bundle args = new Bundle();
//            args.putInt("channel", channelId);
//            args.putInt("order_by", 0);
//            NewsListFragment fragment = (NewsListFragment) NewsListFragment
//                    .instantiate(getActivity(), NewsListFragment.class.getName(), args);
//            mFragmentMap.put(channelId, fragment);
//            return fragment;
            return null;
        }

        @Override
        public int getCount() {
            return mNewsTypesResponse == null ? 0 : mNewsTypesResponse.data.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mNewsTypesResponse.data[position].name;
        }

    }

    public static class NewsTypesResponse extends BaseResponse {

        public static class NewsType {

            public int id;

            public String name;
        }

        public NewsType[] data;
    }

    private NewsTypesResponse mNewsTypesResponse;

    private NewsChannelAdapter mChannelAdapter;

    private void loadChannels() {
        GsonRequest<NewsTypesResponse> request = new GsonRequest<NewsTypesResponse>(
                NetworkConstants.NEWS_TYPES_URL, Request.Method.GET, NewsTypesResponse.class, false,
                null, new Response.Listener<NewsTypesResponse>() {
            @Override
            public void onResponse(NewsTypesResponse response) {
                if (!response.ret) {
                    Toast.makeText(getActivity(), response.msg, Toast.LENGTH_SHORT).show();
                }
                mNewsTypesResponse = response;
                mChannelAdapter.notifyDataSetChanged();
                mTabs.setViewPager(mPager);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
//                Toast.makeText(getActivity(), error.getLocalizedMessage(), Toast.LENGTH_SHORT)
//                        .show();
            }
        });
        NetworkHelper.getInstance().addToRequestQueue(request);
        NetworkHelper.getInstance().getRequestQueue().start();
    }
}
